package dao

import (
	"golang-websocket-chatroom/global"
	"golang-websocket-chatroom/internal/model"
)

func CreateUser(user *model.Users) bool {
	if err := global.DB.Create(&user).Error; err != nil {
		return false
	}
	return true
}
func GetUser(tel string) model.Users {
	var result model.Users
	if err := global.DB.Where("tel=?", tel).Find(&result).Error; err != nil {
		return model.Users{}
	}
	return result
}

func Chat(test string) model.Chat {
	var result model.Chat
	err := global.DB.Where("test = ?", test).Find(&result).Error
	if err != nil {
		return model.Chat{}
	}
	return result
}
func ListUser() []model.Users {
	var result []model.Users
	if err := global.DB.Find(&result).Error; err != nil {
		return []model.Users{}
	}
	return result
}

func GetOneById[T any](id uint, data *T) bool {
	if err := global.DB.Where("id = ?", id).Find(data).Limit(1).Error; err != nil {
		return false
	}
	return true
}
func ListChat() []model.Chat {
	var result []model.Chat
	if err := global.DB.Find(&result).Error; err != nil {
		return []model.Chat{}
	}
	return result
}
func ListRoom() []model.Room {
	var result []model.Room
	if err := global.DB.Find(&result).Error; err != nil {
		return []model.Room{}
	}
	return result
}
func CreateRoom(room *model.Room) bool {
	if err := global.DB.Create(&room).Error; err != nil {
		return false
	}
	return true
}
